//
//  KARegistVC.m
//  KeepAccounts
//
//  Created by honeyrr on 2017/11/9.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "KARegistVC.h"


@interface KARegistVC ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTF;
@property (weak, nonatomic) IBOutlet UIButton *registBtn;

@end

@implementation KARegistVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"新用户注册";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpUI];
}

-(void) setUpUI
{
    [self setTFLeftLalelName:@"用户名:" toTF:_userNameTF];
    [self setTFLeftLalelName:@"手机号:" toTF:_phoneNumTF];
    [self setTFLeftLalelName:@"新密码:" toTF:_pwdTF];
    [self setTFLeftLalelName:@"确认密码:" toTF:_confirmPwdTF];
}
-(void) setTFLeftLalelName:(NSString *)name toTF:(UITextField *)tf
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 44)];
    UILabel *leftLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 75, 44)];
    leftLable.center = view.center;
    leftLable.text = name;
    leftLable.font = [UIFont systemFontOfSize:16];
    [view addSubview:leftLable];
    tf.leftViewMode = UITextFieldViewModeAlways;
    tf.rightViewMode = UITextFieldViewModeAlways;
    tf.leftView = view;
    
    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 44)];
    rightLabel.text = @"*必填";
    rightLabel.font = [UIFont systemFontOfSize:12];
    rightLabel.textColor = [UIColor redColor];
    
    tf.rightView = rightLabel;
}
- (IBAction)registBtn:(id)sender {
    NSArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"users"];
    NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
    if ([_userNameTF.text isEqualToString:@""] && [_phoneNumTF.text isEqualToString:@""] && [_pwdTF.text isEqualToString:@""] && [_confirmPwdTF.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"请填写完信息"];
    } else {
        BOOL isSame = false;
        for (NSDictionary *dict in marr) {
            if ([dict[@"userName"] isEqualToString:_userNameTF.text]) {
                isSame = true;
            }
        }
        if (isSame) {
            [SVProgressHUD showErrorWithStatus:@"该用户已存在"];
        } else {
            if (![_pwdTF.text isEqualToString:_confirmPwdTF.text]) {
                [SVProgressHUD showErrorWithStatus:@"两次输入的密码不匹配"];
            } else {
                NSDictionary *dict = @{@"userName":_userNameTF.text, @"phoneNum":_phoneNumTF.text, @"password":_pwdTF.text};
                [marr addObject:dict];
                [[NSUserDefaults standardUserDefaults] setObject:marr forKey:@"users"];
                [SVProgressHUD showSuccessWithStatus:@"注册成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    }
}

@end
